﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-

'get object information'

__author__='zhaoxp'

import types

class student(object):
	pass

class Animal(object):
	pass

class Dog(Animal):
	pass
	
class Cat(Animal):
	pass
	
def testFunction():
	pass
	
class MyObject(object):
	def __init__(self):
		self.x=7
		self.__m=8
	def __len__(self):
		return 21
	def power(self):
		return self.x*self.x
	
	
if __name__=='__main__':
	print "get object information example"
	print "---------- type usage ----------"
	print "type(None) =",type(None)
	print "type(123) =",type(123)
	print "type(3.1415926) =",type(3.1415926)
	print "type('hello') =",type('hello')
	print "type(u'hello') =",type(u'hello')
	print "type([1,2,3]) =",type([1,2,3])
	print "type((1,2,3)) =",type((1,2,3))
	print "type({1:'a',2:'b',3:'c'}) =",type({1:'a',2:'b',3:'c'})
	print "type(student) =",type(student)
	print "type(student()) =",type(student())
	print "type(testFunction) =",type(testFunction)
	print "types =",types
	#print "dir(types) =",dir(types)
	print "types.IntType =",types.IntType
	print "type(123)==types.IntType =",type(123)==types.IntType
	print "type('hello')==types.StringType =",type('hello')==types.StringType
	print "type(u'hello')==types.UnicodeType =",type(u'hello')==types.UnicodeType
	print "type([1,2,3])==types.ListType =",type([1,2,3])==types.ListType
	print "type((1,2,3))==types.TupleType =",type((1,2,3))==types.TupleType
	print "type(testFunction)==types.FunctionType =",type(testFunction)==types.FunctionType
	print "type(type)==type.TypeType =",type(type)==types.TypeType
	print "---------- isinstance usage ----------"
	print "isinstance(123,int) =",isinstance(123,int)
	print "isinstance(3.1415926,float) =",isinstance(3.1415926,float)
	print "isinstance('hello',str) =",isinstance('hello',str)
	print "isinstance(u'hello',unicode) =",isinstance(u'hello',unicode)
	print "isinstance('hello',basestring) =",isinstance('hello',basestring)
	print "isinstance(u'hello',basestring) =",isinstance(u'hello',basestring)
	print "isinstance([1,2,3],list) =",isinstance([1,2,3],list)
	print "isinstance((1,2,3),tuple) =",isinstance((1,2,3),tuple)
	print "isinstance({1:'a',2:'b',3:'c'},dict) =",isinstance({1:'a',2:'b',3:'c'},dict)
	a=Animal()
	d=Dog()
	c=Cat()
	print "isinstance(a,Animal) =",isinstance(a,Animal)
	print "isinstance(d,Dog) =",isinstance(d,Dog)
	print "isinstance(c,Cat) =",isinstance(c,Cat)
	
	print "isinstance(a,Dog) =",isinstance(a,Dog)
	print "isinstance(a,Cat) =",isinstance(a,Cat)
	print "isinstance(d,Animal) =",isinstance(d,Animal)
	print "isinstance(d,Cat) =",isinstance(d,Cat)
	print "isinstance(c,Animal) =",isinstance(c,Animal)
	print "isinstance(c,Dog) =",isinstance(c,Dog)
	
	print "issubclass(Dog,Anima) =",issubclass(Dog,Animal)
	
	# 如果要获得一个对象的所有属性和方法，可以使用dir()函数，它返回一个包含字符串的list，
	# 比如，获得一个str对象的所有属性和方法：
	print "---------- dir usage ----------" 
	print "dir('hello') =",dir('hello')
	obj=MyObject()
	# 对象的id
	print "id(obj) =",id(obj)
	print "obj=MyObject() =",obj
	print "len(obj) =",len(obj)
	print "obj.x =",obj.x
	print "hasattr(obj,'x') =",hasattr(obj,'x')
	print "hasattr(obj,'y') =",hasattr(obj,'y')
	print "hasattr(obj,'__m') =",hasattr(obj,'__m')
	print "getattr(obj,'x')",getattr(obj,'x')
	print "setattr(obj,'x',15)",setattr(obj,'x',15)
	print "obj.x =",obj.x
	# 可以传入一个default参数，如果属性不存在，就返回默认值
	print "getattr(obj,'z',404) =",getattr(obj,'z',404)
	# 也可以获得对象的方法：
	print "hasattr(obj,'power') =",hasattr(obj,'power')
	print "getattr(obj,'power') =",getattr(obj,'power')
	fn=getattr(obj,'power')
	print "fn=getattr(obj,'power') =",fn
	print "type(fn) =",type(fn)
	print "fn() =",fn()
	obj.z=12
	print "obj.z=12"
	print "hasattr(obj,'z') =",hasattr(obj,'z')
	delattr(obj,'z')
	print "delattr(obj,'z')"
	print "hasattr(obj,'z')",hasattr(obj,'z')
	
""" 
dir(types)=

BooleanType
BufferType
BuiltinFunctionType
BuiltinMethodType
ClassType
CodeType
ComplexType
DictProxyType
DictType
DictionaryType
EllipsisType
FileType
FloatType
FrameType
FunctionType
GeneratorType
GetSetDescriptorType
InstanceType
IntType
LambdaType
ListType
LongType
MemberDescriptorType
MethodType
ModuleType
NoneType
NotImplementedType
ObjectType
SliceType
StringType
StringTypes
TracebackType
TupleType
TypeType
UnboundMethodType
UnicodeType
XRangeType
__builtins__
__doc__
__file__
__name__
__package__
"""